<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class Suggest extends Common {

	public function index() {
		$res = Db::table('cn_suggest')
			->alias('cs')
			->join('cn_user cu', 'cu.user_id = cs.create_user_id')
			->where('cu.is_delete', 0)
			->where('cs.is_delete', 0)
			->field('cu.user_name,cu.user_id,cs.create_time,cs.sug_title,cs.sug_id,sug_content')
			->order('cs.create_time', 'desc')
		// ->select();
			->paginate(5);
		$page = $res->render();
		return view('index@Suggest/index', [
			'res' => $res,
			'page' => $page,
		]);

	}

	public function run() {
		$uid = $_SESSION['think']['username'][0]['user_id'];
		$data = [
			'sug_title' => $_POST['title'],
			'sug_content' => $_POST['post_content'],
			'is_delete' => 0,
			'create_time' => date('Y-m-d H:i:s'),
			'create_user_id' => $uid,
		];
		$res = Db::table('cn_suggest')
			->insert($data);
		if (isset($res)) {
			$this->success('留言成功');
		} else {
			$this->error('留言失败');
		}
	}

	public function del($id) {
		$data = [
			'is_delete' => 1,
		];
		$res = Db::table('cn_suggest')
			->where('sug_id', $id)
			->update($data);

		if (isset($res)) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}

	public function getcontent($id) {
		$res = DB::table('cn_suggest')
			->where('sug_id', $id)
			->find();
		return $res;
	}

}
